home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0077_Number of Days in a month.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  1.6 KB  |  47 lines

  1. {
  2. Since Delphi's TDateTime data type stores dates in
  3. number of days since 1/1/0001, all you need to do is
  4. subtract the TDateTime of the first day of the month
  5. in question from the TDateTime of the first day of the
  6. following month.
  7.  
  8. The following function will return an integer representing
  9. the number of days in any given month:
  10. }
  11.  
  12. function daysHathTheMonth(whichMonth, whichYear:integer):integer;
  13. var
  14.    beginDate, endDate :TDateTime;
  15.    beginMonth,endMonth,beginYear,endYear:word;
  16. begin
  17.    beginMonth := whichMonth;
  18.    beginYear := whichYear;
  19.    endMonth := whichMonth + 1;
  20.    endYear := whichYear;
  21.    if beginMonth > 11 then
  22.       begin
  23.           {the month in question is December, so the following
  24.                TDateTime would be January 1st of the following year}
  25.            beginMonth := 12;
  26.            endMonth := 1;
  27.            endYear := whichYear + 1;
  28.       end;
  29.    beginDate := encodeDate(beginYear,beginMonth,01);
  30.    endDate := encodeDate(endYear,endMonth,01);
  31.    daysHathTheMonth:= strToInt(floatToStr(endDate - beginDate));
  32. end;
  33. ==============================================================
  34.  
  35. To test it, just call:
  36.  showMessage(intToStr(daysHathTheMonth(04,1996)));
  37.  
  38. You can easily add a case statement in order to be able to
  39. pass the function a string such as 'Feb', etc. if you
  40. want, but I find integers/words more convenient since
  41. all of Delphi's Date and Time Routines use them.
  42.  
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44.  Carl Steinhilber         csteinhilber@graphicmedia.com
  45. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  46.  
  47.